from flask import Blueprint, request, render_template, flash, redirect, url_for, jsonify

from services.product_service import ProductService
from utils import response
from models.admin_user import AdminUser
from services.adminuser_service import AdminUserService
from models.product import Product

# 创建蓝图
product_api_bp = Blueprint('product_api', __name__)
BASE_URL = 'http://127.0.0.1:8000/'


@product_api_bp.route('/')
def product_img():
    products = Product.query.all()

    # img_url = 'http://127.0.0.1:5000/uploads/products/'+
    product_list = [product.to_dict() for product in products]
    return product_list

@product_api_bp.route('/detail/<int:id>')
def product_detail(id):
    product = Product.query.get(id)
    product_dict = product.to_dict()

    # 将字典放入列表（根据需求可选）
    product_list = [product_dict]

    return product_list

@product_api_bp.route('/admin/product/add', methods=['POST'])
def add_product():
    name = request.form.get('name')
    desc = request.form.get('desc')
    category = request.form.get('category')
    quality = request.form.get('quality')
    specification = request.form.get('specification')
    url = request.files.get('url')

    try:
        ProductService.add(name, desc, category, quality, specification,url)
        return jsonify({"message": "产品添加成功"}), 200
    except Exception as e:
        return jsonify({"message": str(e)}), 500
